Set modules path first:
In [1]:
import sys
#sys.path.insert(0, '/home/asanso/workspace/att-spyder/att/src/python/')
sys.path.insert(0, 'i:/dev/workspaces/python/att-workspace/att/src/python/')
The main abstract base class is the following one:
class SerialPort: metaclass = abc.ABCMeta
@abc.abstractmethod
def isOpen(self):
pass
@abc.abstractmethod
def readline(self):
pass
@abc.abstractmethod
def close(self):
pass
@abc.abstractmethod
def get_port(self):
return ""
@abc.abstractmethod
def get_baudrate(self):
return 0
As an example, we can see a dummy implementation:
class DummySerialPort (SerialPort): def init(self, port = None, baud = None): pass
def isOpen(self):
return True
def close(self):
pass
def get_port(self):
return ""
def get_baudrate(self):
return 0
def readline(self):
time_delay = int(3*random.random())+1
time.sleep(time_delay)
return self.gen_random_line()
def gen_random_line(self):
return "Hee"
In order to build an instance of a SerialPort class, we have 2 options:
In [2]:
import hit.serial.serial_port
port=""
baud=0
dummySerialPort = hit.serial.serial_port.DummySerialPort(port, baud)
The DummSerialPort is very simple. It just says "Hee" (after a few seconds) when its method "readline()" is called.
Port and Baud are useless here.
In [3]:
print dummySerialPort.readline()
Let's create a more interesting Serialport instance,
In [4]:
import hit.serial.serial_port
port=""
baud=0
emulatedSerialPort = hit.serial.serial_port.ATTEmulatedSerialPort(port, baud)
The ATTEmulatedSerialPort will emulate a real ATT serial port reading.
Port and Baud are useless here.
In [5]:
print emulatedSerialPort.readline()
Let's use a builder now. We can choose the builder we want and build as many SerialPorts we want.
In [6]:
import hit.serial.serial_port_builder
builder = hit.serial.serial_port_builder.ATTEmulatedSerialPortBuilder()
port=""
baud=0
emulatedSerialPort1 = builder.build_serial_port(port, baud)
emulatedSerialPort2 = builder.build_serial_port(port, baud)
emulatedSerialPort3 = builder.build_serial_port(port, baud)
emulatedSerialPort4 = builder.build_serial_port(port, baud)
emulatedSerialPort5 = builder.build_serial_port(port, baud)
emulatedSerialPort6 = builder.build_serial_port(port, baud)
emulatedSerialPort7 = builder.build_serial_port(port, baud)
And call "readline()"
In [7]:
print emulatedSerialPort5.readline()
There is a special Serial port abstraction that is fed from a file.
This is useful when we want to "mock" the serial port and give it previously stored readings.
This is interesting, for example, in order to reproduce, or visualize the repetition of an interesting set of hits in a game. Because Serial line is Real-Time, there are situations where it is needed to provide the ATT framework with a set of know hits, previously stored.
We can use the data use in "Train points importer".
In [8]:
!head -10 train_points_import_data/arduino_raw_data.txt
In [9]:
import hit.serial.serial_port_builder
builder = hit.serial.serial_port_builder.ATTHitsFromFilePortBuilder()
port="train_points_import_data/arduino_raw_data.txt"
baud=0
fileSerialPort = builder.build_serial_port(port, baud)
And now we will read some lines:
In [12]:
for i in range(20):
print fileSerialPort.readline()
In [ ]: